package jdbc;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TestJDBC1 {
    public static void main(String[] args) throws SQLException {
        //1.创建数据源对象
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test9_3?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("111111");

        //2.和数据服务器建立网络连接
        Connection connection = dataSource.getConnection();
        //System.out.println(connection);

        //3.构造 SQL 语句
        String sql = "insert into student values(1,'张三','m')";
        PreparedStatement statement = connection.prepareStatement(sql);

        //4.把 SQL 发送到服务器上进行执行
        //返回值表示,这个SQL影响到几行
        int n = statement.executeUpdate();
        System.out.println(n);

        //5.释放上述的资源
        statement.close();
        connection.close();
    }
}
